{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Python3内置的随机库：random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 获取随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.randint(1,9)  # 获取范围内的随机整数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9.312691356384095"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.uniform(1.1,9.8)  # 获取范围内的随机小数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 随机取值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1,2,3,4,5,'a','b','c','d']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'b'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.choice(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b 5 3 1 4 4 a b b d d c 3 d 2 4 3 1 4 1 3 1 2 d d a 3 4 5 1 3 d 4 b c d 3 1 3 b b b 3 3 c 4 1 d 4 1 3 2 5 a 4 4 a a a 1 3 a d 2 4 2 4 1 d 5 2 1 b b c 2 2 a 4 a 2 b b 4 4 1 3 c 3 d c 4 5 2 d c 2 5 5 "
     ]
    }
   ],
   "source": [
    "for i in range(1,100):\n",
    "    print(random.choice(a),end=' ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# random.randrange(start,end) # random.choice(range(start,end)) 两个函数的简写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.randrange(1,10) # random.choice(range(start,end))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 打乱顺序[搞破坏]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'b', 'c', 'd', 'e', 'f', 'g']\n"
     ]
    }
   ],
   "source": [
    "s = list('abcdefg')\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "random.shuffle(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['f', 'e', 'b', 'a', 'c', 'g', 'd']"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'b', 'c', 'd', 'e', 'f', 'g']\n",
      "\n",
      "\n",
      "['g', 'd', 'a', 'f', 'c', 'e', 'b']\n",
      "['b', 'g', 'd', 'e', 'c', 'a', 'f']\n",
      "['d', 'f', 'c', 'e', 'b', 'a', 'g']\n",
      "['f', 'd', 'c', 'g', 'b', 'e', 'a']\n"
     ]
    }
   ],
   "source": [
    "s = list('abcdefg')\n",
    "print(s)\n",
    "print('\\n')\n",
    "for i in range(1,5):\n",
    "    random.shuffle(s)\n",
    "    print(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 随机选择指定长度的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[7, 5, 1]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.sample([1,2,3,4,5,6,7],3)  # random.sample(集合，长度值)  返回列表，长度为传入的长度值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "Sample larger than population or is negative",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-28-766114515d5a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mrandom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msample\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m  \u001b[1;31m#集合的数据个数必须大于传入的长度值\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mc:\\python36\\lib\\random.py\u001b[0m in \u001b[0;36msample\u001b[1;34m(self, population, k)\u001b[0m\n\u001b[0;32m    316\u001b[0m         \u001b[0mn\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpopulation\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    317\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;36m0\u001b[0m \u001b[1;33m<=\u001b[0m \u001b[0mk\u001b[0m \u001b[1;33m<=\u001b[0m \u001b[0mn\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 318\u001b[1;33m             \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Sample larger than population or is negative\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    319\u001b[0m         \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mk\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    320\u001b[0m         \u001b[0msetsize\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m21\u001b[0m        \u001b[1;31m# size of a small set minus size of an empty list\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mValueError\u001b[0m: Sample larger than population or is negative"
     ]
    }
   ],
   "source": [
    "random.sample([1,2],3)  #集合的数据个数必须大于等于传入的长度值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[5, 7, 2, 4]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.sample([1,2,3,4,5,6,7],4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 1, 3, 4]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.sample([1,2,3,4],4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 1, 4, 2]\n",
      "[4, 2, 3, 1]\n",
      "[3, 1, 4, 2]\n",
      "[3, 1, 4, 2]\n",
      "[3, 2, 4, 1]\n",
      "[4, 3, 2, 1]\n",
      "[4, 3, 2, 1]\n",
      "[4, 2, 1, 3]\n",
      "[1, 2, 4, 3]\n",
      "[2, 1, 4, 3]\n",
      "[1, 3, 4, 2]\n",
      "[3, 2, 1, 4]\n",
      "[4, 2, 1, 3]\n",
      "[2, 3, 1, 4]\n",
      "[2, 4, 1, 3]\n",
      "[3, 1, 4, 2]\n",
      "[4, 3, 2, 1]\n",
      "[1, 3, 4, 2]\n",
      "[2, 1, 4, 3]\n",
      "[1, 3, 4, 2]\n",
      "[3, 4, 1, 2]\n",
      "[3, 4, 2, 1]\n",
      "[3, 4, 1, 2]\n",
      "[3, 4, 1, 2]\n",
      "[4, 2, 3, 1]\n",
      "[1, 4, 2, 3]\n",
      "[1, 3, 2, 4]\n",
      "[4, 3, 2, 1]\n",
      "[3, 1, 4, 2]\n",
      "[2, 1, 3, 4]\n",
      "[3, 1, 2, 4]\n",
      "[3, 2, 4, 1]\n",
      "[3, 2, 4, 1]\n",
      "[1, 4, 2, 3]\n",
      "[4, 1, 3, 2]\n",
      "[3, 4, 2, 1]\n",
      "[4, 2, 3, 1]\n",
      "[3, 1, 4, 2]\n",
      "[2, 3, 1, 4]\n",
      "[2, 3, 4, 1]\n",
      "[3, 4, 2, 1]\n",
      "[2, 4, 1, 3]\n",
      "[2, 1, 3, 4]\n",
      "[2, 4, 1, 3]\n",
      "[2, 3, 4, 1]\n",
      "[4, 2, 3, 1]\n",
      "[3, 1, 2, 4]\n",
      "[3, 1, 2, 4]\n",
      "[3, 4, 1, 2]\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,50):   #sample 操作，返回值不会有重复的数据，数据具有唯一性\n",
    "    print(random.sample([1,2,3,4],4))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
